#!/bin/bash
# author    tyb


# 函数
function isgreen() {
    if [ $? -eq 0 ]; then
        echo -e "\e[32m$1==========>succeed\e[0m"
    else
        echo -e "\e[31m$1==========>failed\e[0m"
        exit 1
    fi
}

function network() {
    #超时时间
    local timeout=1

    #目标网站
    local target=www.baidu.com

    #获取响应状态码
    local ret_code
    ret_code=$(curl -I -s --connect-timeout ${timeout} ${target} -w "%{http_code}" | tail -n1)

    if [ "$ret_code" = "200" ]; then
        #网络畅通
        echo -e "\e[32m网络畅通==========>succeed\e[0m"
        return 0
    else
        #网络不畅通
        echo -e "\e[31m网络不畅通，请检查网络==========>succeed\e[0m"
        return 1
    fi

}

function print_help() {
  echo "Usage: $0 [OPTIONS]"
  echo "Options:"
  echo "  --help,-h           Display this help message."
  echo ""

  echo "ps: docker version is 20.10.24 !!!"
  
}

# 获取主机名，需要输入用户名
while [[ $# -gt 0 ]]; do
  case "$1" in
    --help| -h)
       print_help
        exit 0
        ;;
    # --hostname)
    #   if [[ -n "$2" ]]; then
    #     host="$2"
    #     shift 2
    #   else
    #     echo "Missing argument for -h option."
    #     exit 1
    #   fi
    #   ;;
    *)
      echo "Invalid option: $1"
      exit 1
      ;;
  esac
done

# 全局变量
# hostname=${host:-"master"}
# ip=$(ip addr | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | awk -F "/" '{print $1}')




# # 修改主机名为获取到的主机名
# echo "修改主机名"
# hostnamectl set-hostname "$hostname"
# isgreen "修改主机名为：$hostname"

# # 配置hosts解析文件
# echo "配置hosts解析"
# if grep -q "$hostname" /etc/hosts; then
#     #解析存在进行修改
#     sed -i "s/.*$hostname.*/$ip $hostname/g" /etc/hosts
#     ping "$hostname" -c 1 >/dev/null 2>&1
#     isgreen "配置hosts解析"
# else
#     # 解析不存在就新添加
#     echo "$ip $hostname" >>/etc/hosts
#     ping "$hostname" -c 1 >/dev/null 2>&1
#     isgreen "配置hosts解析"
# fi

# 关闭防火墙和selinux
echo "关闭防火墙和selinux"
systemctl disable firewalld --now >/dev/null 2>&1
isgreen "关闭防火墙"
echo "关闭selinux"
if [ "$(getenforce)" != "Enforcing" ];then
    echo -e "\e[32m关闭selinux==========>succeed\e[0m"
else
    setenforce 0
    isgreen "临时关闭"
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    isgreen "永久关闭"
fi

# 关闭交换分区
echo "关闭交换分区"
swapoff -a
isgreen "临时关闭交换分区"
sed -i '/swap/s/^/#/' /etc/fstab
isgreen "永久关闭交换分区"

# 修改内核参数
echo "加载内核模块"
modprobe br_netfilter
if ! grep -q modprobe /etc/profile; then
    echo "modprobe br_netfilter" >>/etc/profile
fi
isgreen "加载内核模块"
echo "修改内核参数"
if [ ! -f "/etc/sysctl.d/k8s.conf" ]; then
    cat >/etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
fi
sysctl -p /etc/sysctl.d/k8s.conf >/dev/null 2>&1
isgreen "修改内核参数"

# 开启ipvs
echo "开启ipvs"
ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack"
for kernel_module in ${ipvs_modules}; do
 /sbin/modinfo -F filename "${kernel_module}" > /dev/null 2>&1
 /sbin/modprobe "${kernel_module}"
done
lsmod | grep ip_vs >/dev/null 2>&1
isgreen "开启ipvs"

# 配置yum源
echo "配置yum源"
network
if [ ! -f "/etc/yum.repos.d/docker.repo" ]; then
    rm -rf /etc/yum.repos.d/*
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo >/dev/null 2>&1
    isgreen "aliyun-base源"
    curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo >/dev/null 2>&1
    isgreen "aliyun-epel源"
    curl -o /etc/yum.repos.d/docker.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo >/dev/null 2>&1
    isgreen "aliyun-docker源"
    cat >/etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF
    isgreen "aliyun-k8s源"
else
    echo -e "\e[32m配置yum源==========>succeed\e[0m"
fi

# 配置时间同步
echo "配置时间同步"
if ! rpm -q ntpdate >/dev/null 2>&1; then
    yum install -y ntpdate >/dev/null 2>&1
    isgreen "install ntpdata"
else
    echo >/dev/null
    isgreen "install ntpdata"
fi
if [ ! -f "/var/spool/cron/root" ]; then
    echo "* */1 * * * /usr/sbin/ntpdate   cn.pool.ntp.org" >>/var/spool/cron/root
    isgreen "添加定时任务"
fi
systemctl restart crond >/dev/null 2>&1
isgreen "重启crond"
ntpdate cn.pool.ntp.org >/dev/null 2>&1
isgreen "配置时间同步"


# 安装docker
echo "安装docker"
if ! rpm -q docker-ce-20.10.24-3.el7.x86_64 >/dev/null; then
    yum install -y docker-ce-20.10.24-3.el7.x86_64 docker-ce-cli-20.10.24-3.el7.x86_64 >/dev/null 2>&1
    isgreen "install docker"
else
    echo >/dev/null
    isgreen "install docker"
fi
systemctl enable docker --now >/dev/null 2>&1
isgreen 启动docker
if [ ! -f "/etc/docker/daemon.json" ]; then
    cat >/etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://t37y9kai.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
    isgreen 配置加速器
fi
sudo systemctl daemon-reload
sudo systemctl restart docker
isgreen 重启docker